﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
using System.IO;
using ThoiTrang.Business;

public partial class emailFriend : System.Web.UI.Page
{
    protected Regex r = new Regex(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
    protected string link = string.Empty;
    protected bool isMessageMode = false;
    protected override void OnInit(EventArgs e)
    {
        btnSubmit.Click += new EventHandler(btnSubmit_Click);
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetLink();
        }
        if (Request["dt"] == null && Request["aid"]==null)
        {
            divEmail.Visible = false;
            divMsg.Visible = true;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (ValidateEmail())
        {
            StreamReader reader = new StreamReader(Server.MapPath("EmailTemplates/EmailFriend.htm"));
            string content = reader.ReadToEnd();
            content = content.Replace("[SenderName]", ten_nguoigui.Value.Trim());
            content = content.Replace("[Link]", lnkPage.InnerHtml);
            content = content.Replace("[Message]", noidung.Value.Trim());
            content = content.Replace("[ReceiverName]", ten_nguoinhan1.Value.Trim());
            
            string subject = String.Format("{0} ({1}) moi ban xem thoitrang.com", ten_nguoigui.Value.Trim(), email_nguoigui.Value.Trim());
            EmailHelper.SendMailToFriend(subject, ten_nguoigui.Value.Trim(), email_nguoigui.Value.Trim(), email_nguoinhan1.Value.Trim(), content);

            SendMailToAFriend(subject, content, ten_nguoinhan2.Value.Trim(), email_nguoinhan2.Value.Trim());
            SendMailToAFriend(subject, content, ten_nguoinhan3.Value.Trim(), email_nguoinhan3.Value.Trim());
            //Response.Redirect("emailFriend.aspx?aid=" + Request["aid"] + "&ckid=" + SessionManager.GetCookie());
            divEmail.Visible = false;
            divMsg.Visible = true;
            txtThanks.InnerHtml = "Cảm ơn " + ten_nguoigui.Value + ". Thư của bạn đã được gửi đến " + email_nguoinhan1.Value + ".";
        }
        else
        {
            lbMsg.InnerHtml = "<p>&nbsp;</p> <p>&nbsp;Xin vui lòng nhập lại email.</p> <p>&nbsp;</p>";
            email_nguoinhan1.Value = string.Empty;
            email_nguoinhan2.Value = string.Empty;
            email_nguoinhan3.Value = string.Empty;
        }
    }

    protected void SendMailToAFriend(string subject, string content, string recieverName, string receiverEmail)
    {
        if (receiverEmail.Trim() != "" && r.IsMatch(receiverEmail.Trim()))
        {
            content = content.Replace("[ReceiverName]", receiverEmail);
            EmailHelper.SendMailToFriend(subject, ten_nguoigui.Value.Trim(), email_nguoigui.Value.Trim(), receiverEmail, content);
        }
    }

    protected void GetLink()
    {
        try
        {
            link = (String.IsNullOrEmpty(Request.ServerVariables["HTTP_REFERER"]))?"":Request.ServerVariables["HTTP_REFERER"];
            string firstSubLink = string.Empty;
            string secondSubLink = string.Empty;
            if (link.Contains("detail_print"))
                link = link.Replace("detail_print", "detail");
            if (link.Contains("ckid"))
            {
                int id = link.IndexOf("ckid");
                firstSubLink = link.Substring(0, id);
                if(link.Substring(id-1,1)=="&") firstSubLink = firstSubLink.Remove(id-1,1);

                if (link.Substring(id).Contains("&"))
                {
                    int index = link.Substring(id).IndexOf('&');                    
                    secondSubLink = link.Substring(index + 1);
                }
                link = string.Concat(firstSubLink, secondSubLink);
            }
            lnkPage.InnerHtml = link;
        }
        catch (Exception ex)
        {
        }
    }

    protected bool ValidateEmail()
    {        
        if (email_nguoigui.Value.Trim() == "" || !r.IsMatch(email_nguoigui.Value))
            return false;
        else if (email_nguoinhan1.Value.Trim() == "" || (email_nguoinhan1.Value.Trim() != "" && !r.IsMatch(email_nguoinhan1.Value)))
            return false;
        return true;
    }
}
